﻿using System;
using System.Collections.Generic;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class DbCellRecord : BiffRecord
    {
        private uint dbRtrw;
        private List<ushort> rgdb = new List<ushort>();

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            this.dbRtrw = context.ReadUInt32();
            int count = (length - 4)/2;
            for (int i = 0; i < count; i++)
                rgdb.Add(context.ReadUInt16());
        }


        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.DBCell; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            var position = context.Position - 2 - 2;
            if (context.DBCellPosition != null)
            {
                using (MovedContext s = context.CreateMovedContext(context.DBCellPosition.Value))
                {
                    context.WriteUInt32((uint) position);
                }
                context.DBCellPosition += 4;
            }

            context.Writer.Write(dbRtrw);
            foreach (var item in rgdb)
            {
                context.Writer.Write(item);
            }
        }
    }
}